<?php


namespace app\engine\controller;


use cmf\controller\HomeBaseController;
use think\Db;

class LifeController extends HomeBaseController
{

    public function index()
    {
        $data = $this->request->param();
        if (empty($data['id'])) {
            $this->error('缺少必要参数');
        }
        $id = $data['id'];
        $res = Db::name('life')->where(['eid' => $id])->group('type')->field('type')->select()->toArray();
        $all = Db::name('life')->where(['eid' => $id])->sum('price');
        $sup = Db::name('life')->where(['eid' => $id, 'type' => 2])->sum('price');
        $lea = Db::name('life')->where(['eid' => $id, 'type' => 1])->sum('price');
        $this->assign('all', $all);
        $this->assign('sup', $sup);
        $this->assign('lea', $lea);
        $this->assign('res', $res);
        $this->assign('id', $id);
        return $this->fetch();
    }

    public function indexs()
    {
        $data = $this->request->param();
        $res = Db::name('life')->where($data)->group('lid')->select()->toArray();
        foreach ($res as $k => &$v) {
            if ($data['type'] == 1) {
                $v['lName'] = Db::name('leader')->where(['id' => $v['lid']])->value('name');
            } else {
                $v['lName'] = Db::name('supplier')->where(['id' => $v['lid']])->value('name');
            }
        }
        $all = $res;
        foreach ($all as $k => &$v) {
            $v['all'] = Db::name('life')->where(['lid' => $v['lid'], 'type' => $v['type']])->sum('price');
        }
        $this->assign('all', $all);
        $this->assign('res', $res);
        $this->assign('id', $data['eid']);
        $this->assign('type', $data['type']);
        return $this->fetch();
    }

    public function add()
    {
        $data = $this->request->param();
        $id = $data['id'];
        $leader = Db::name('leader')->where(['eid' => $id])->field('id,name,type')->select()->toArray();
        //$type = Db::name('leader')->where(['eid' => $id])->field('id,type')->select();
        $this->assign('leader', $leader);
        //$this->assign('type', $type);
        $this->assign('id', $id);
        return $this->fetch();
    }

    public function addsup()
    {
        $data = $this->request->param();
        $id = $data['id'];
        $leader = Db::name('supplier')->where(['able' => 1])->field('id,name,attrName')->select()->toArray();
        // $type = Db::name('supplier')->where(['able' => 1])->field('id,attrName')->select()->toArray();
        $this->assign('leader', $leader);
        // $this->assign('type', $type);
        $this->assign('id', $id);
        return $this->fetch();
    }

    public function doadd()
    {
        $data = $this->request->param();
        $files = request()->file('img');
        unset($data['img']);
        if (empty($data['lid']) || empty($data['price']) || empty($data['payDate']) || empty($data['eid'])) {
            $this->error('缺少参数');
        }
        if ($files) {
            foreach ($files as $k => $file) {
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(CMF_ROOT . 'public' . '/' . 'uploads');
                if ($info) {
                    $kps[$k] = '/uploads/' . date('Ymd', time()) . '/' . $info->getFilename();
                } else {
                    $this->error($file->getError());
                }
            }
            $data['img'] = implode(',', $kps);
        }
        $data['createTime'] = time();
        $res = Db::name('life')->insert($data);
        if (!$res) {
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }

    public function look()
    {
        $data = $this->request->param();
        $lid = $data['lid'];
        $eid = $data['eid'];
        $type = $data['type'];
        $res = Db::name('life')->where(['eid' => $eid, 'lid' => $lid, 'type' => $type])->select()->toArray();
        //        dump($res);die;
        //        $res = Db::name('life')->where(['eid' => $eid, 'lid' => $lid])->group('payDate')->field('payDate')->select()->toArray();
        //        foreach ($res as $k => &$v) {
        //            $v['eid'] = $eid;
        //            $v['lid'] = $lid;
        //        }
        foreach ($res as $k => &$v) {
            if ($v['type'] == 1) {
                $v['lname'] = Db::name('leader')->where(['id' => $v['lid']])->value('name');
                $v['name'] = Db::name('leader')->where(['id' => $v['lid']])->value('type');
            } else {
                $v['lname'] = Db::name('supplier')->where(['id' => $v['lid']])->value('name');
                $v['name'] = Db::name('supplier')->where(['id' => $v['lid']])->value('attrName');
            }
            if (!empty($v['img'])) {
                // 处理多图情况
                $imgArray = explode(',', $v['img']);
                $imgWithDomain = [];
                foreach ($imgArray as $img) {
                    if (!empty($img)) {
                        $imgWithDomain[] = 'http://' . $_SERVER['SERVER_NAME'] . $img;
                    }
                }
                $v['img'] = implode(',', $imgWithDomain);
            } else {
                $v['img'] = 'http://' . $_SERVER['SERVER_NAME'];
            }
        }
        $this->assign('id', $eid);
        $this->assign('res', $res);
        return $this->fetch();
    }

    public function see()
    {
        $data = $this->request->param();
        $id = $data['eid'];
        $res = Db::name('life')->where($data)->select()->toArray();
        foreach ($res as $k => &$v) {
            $v['lname'] = Db::name('leader')->where(['id' => $v['lid']])->value('name');
            $v['img'] = 'http://' . $_SERVER['SERVER_NAME'] . $v['img'];
        }
        $this->assign('res', $res);
        $this->assign('id', $id);
        return $this->fetch();
    }

    public function del()
    {
        $data = $this->request->param();
        $check = Db::name('life')->where(['id' => $data['id']])->find();
        $check['img'] = CMF_ROOT . $check['img'];
        if ($check['img'] != CMF_ROOT) {
            if (file_exists($check['img'])) {
                unlink($check['img']);
            }
        }
        $res = Db::name('life')->where(['id' => $data['id']])->delete();
        if (!$res) {
            $this->error('删除失败');
        }
        $this->success('删除成功');
    }

    public function search()
    {
        $data = $this->request->param();
        $id = $data['id'];
        if (!empty($data['status'])) {
            if ($data['status'] == 1) {
                $where['status'] = 1;
            } elseif ($data['status'] == 2) {
                $where['status'] = 2;
            }
        } else {
            $where = [];
        }
        $res = Db::name('life')->where(['eid' => $data['id']])->where($where)->select()->toArray();
        $all = Db::name('life')->where(['eid' => $id])->sum('price');
        $sup = Db::name('life')->where(['eid' => $id, 'type' => 2])->sum('price');
        $lea = Db::name('life')->where(['eid' => $id, 'type' => 1])->sum('price');
        $this->assign('all', $all);
        $this->assign('sup', $sup);
        $this->assign('lea', $lea);
        foreach ($res as $k => &$v) {
            if ($v['type'] == 1) {
                $v['lname'] = Db::name('leader')->where(['id' => $v['lid']])->value('name');
                $v['name'] = Db::name('leader')->where(['id' => $v['lid']])->value('type');
            } else {
                $v['lname'] = Db::name('supplier')->where(['id' => $v['lid']])->value('name');
                $v['name'] = Db::name('supplier')->where(['id' => $v['lid']])->value('attrName');
            }
            $v['img'] = 'http://' . $_SERVER['SERVER_NAME'] . $v['img'];
        }
        $this->assign('res', $res);
        $this->assign('id', $data['id']);
        return $this->fetch();
    }
}
